package userdao

import (
	"vpn-management-server/logger"
	"vpn-management-server/src/dao"
	"vpn-management-server/src/model/usermodel"
)

func AddUser(user *usermodel.User) error {
	sqlStr := "INSERT INTO users(name,password,phone,invitation,email,device_info,certificate,isrevoked,ug_id,timestamp)value(?,?,?,?,?,?,?,?,?,?)"
	if _, err := dao.GMysqlDB.Conn.Exec(sqlStr, user.Name, user.Password, user.Phone, user.Invitation, user.Email, user.DeviceInfo, user.Certificate, user.IsRevoked, user.UGroupID, user.TimeStamp); err != nil {
		logger.Logger.Error(err)
		return err
	}
	return nil
}

func HasUserByName(name string) bool {
	var user usermodel.User
	sqlStr := "SELECT * FROM users where name = ?"
	if err := dao.GMysqlDB.Conn.Get(&user, sqlStr, name); err != nil {
		return false
	}
	return true
}

func HasUserByEmail(email string) bool {
	var user usermodel.User
	sqlStr := "SELECT * FROM users where email = ?"
	if err := dao.GMysqlDB.Conn.Get(&user, sqlStr, email); err != nil {
		return false
	}
	return true
}

func GetUserByName(name string) *usermodel.User {
	var user usermodel.User
	sqlStr := "SELECT * FROM users where name = ?"
	if err := dao.GMysqlDB.Conn.Get(&user, sqlStr, name); err != nil {
		logger.Logger.Error(err)
		return nil
	}
	return &user
}

func GetUserByID(id int) *usermodel.User {
	var user usermodel.User
	sqlStr := "SELECT * FROM users where id = ?"
	if err := dao.GMysqlDB.Conn.Get(&user, sqlStr, id); err != nil {
		logger.Logger.Error(err)
		return nil
	}
	return &user
}

func UpdateUserPasswordAndTimeStamp(name, password, timestamp string) error {
	sqlStr := "UPDATE users SET password = ? ,timestamp = ? where name = ?"
	if _, err := dao.GMysqlDB.Conn.Exec(sqlStr, password, timestamp, name); err != nil {
		logger.Logger.Error(err)
		return err
	}
	return nil
}

func DeleteUserByID(id int) error {
	sqlStr := "DELETE FROM users WHERE id = ?"
	if _, err := dao.GMysqlDB.Conn.Exec(sqlStr, id); err != nil {
		logger.Logger.Error(err)
		return err
	}
	return nil
}

func RevokeUserByID(id int) error {
	sqlStr := "UPDATE users SET isrevoked = ? WHERE id = ?"
	if _, err := dao.GMysqlDB.Conn.Exec(sqlStr, 1, id); err != nil {
		logger.Logger.Error(err)
		return err
	}
	return nil
}

func RecoverUserByID(id int) error {
	sqlStr := "UPDATE users SET isrevoked = ? WHERE id = ?"
	if _, err := dao.GMysqlDB.Conn.Exec(sqlStr, 0, id); err != nil {
		logger.Logger.Error(err)
		return err
	}
	return nil
}

func GetAllUserInfosByUGroupID(ugid int) []*usermodel.UserNameInfo {
	var userNameInfo []*usermodel.UserNameInfo
	sqlStr := "SELECT id,name FROM users where ug_id = ?"
	if err := dao.GMysqlDB.Conn.Select(&userNameInfo, sqlStr, ugid); err != nil {
		logger.Logger.Error(err)
		return nil
	}
	return userNameInfo
}

func GetUGroupUsersByPage(ugid int, pageNo, pageSize int) []*usermodel.User {
	start := (pageNo - 1) * pageSize
	var users []*usermodel.User
	sqlStr := "SELECT * FROM users where ug_id = ? limit ?,? "
	if err := dao.GMysqlDB.Conn.Select(&users, sqlStr, ugid, start, pageSize); err != nil {
		logger.Logger.Error(err)
		return nil
	}
	return users
}

func GetUserCountByUGroupID(ugid int) int {
	var count int
	sqlStr := "SELECT count(*) FROM users where ug_id = ?"
	if err := dao.GMysqlDB.Conn.Get(&count, sqlStr, ugid); err != nil {
		logger.Logger.Error(err)
		return -1
	}
	return count
}

func GetAllUserByUGroupID(ugid int) []*usermodel.User {
	var users []*usermodel.User
	sqlStr := "SELECT * FROM users where ug_id = ?"
	if err := dao.GMysqlDB.Conn.Select(&users, sqlStr, ugid); err != nil {
		logger.Logger.Error(err)
		return nil
	}
	return users
}

func GetAllUser() []*usermodel.User {
	var users []*usermodel.User
	sqlStr := "SELECT * FROM users"
	if err := dao.GMysqlDB.Conn.Select(&users, sqlStr); err != nil {
		logger.Logger.Error(err)
		return nil
	}
	return users
}

func GetAllUserCount() int {
	var count int
	sqlStr := "SELECT COUNT(*) FROM users"
	if err := dao.GMysqlDB.Conn.Get(&count, sqlStr); err != nil {
		logger.Logger.Error(err)
		return -1
	}
	return count
}

func GetUGroupIDByUserName(name string) int {
	var ugid int
	sqlStr := "SELECT ug_id FROM users WHERE name = ?"
	if err := dao.GMysqlDB.Conn.Get(&ugid, sqlStr, name); err != nil {
		logger.Logger.Error(err)
		return -1
	}
	return ugid
}

func GetUserIDByName(name string) int {
	var uid int
	sqlStr := "SELECT id FROM users WHERE name = ?"
	if err := dao.GMysqlDB.Conn.Get(&uid, sqlStr, name); err != nil {
		logger.Logger.Error(err)
		return -1
	}
	return uid
}
